import Mockjs from 'mockjs'
import { param2Obj } from '../utils/myUtils'
Mockjs.mock(/\/logon/, 'get', logonFn)
Mockjs.mock(/\/zhuce/, 'post', zhuceFn)
Mockjs.mock(/\/getuser/, 'post', getUserFn)


let arr = [
    { user: 'xiaoming', name: '小明', password: '123456', token: 'xiaoming------token----', power: '用户', headP: Mockjs.mock('Random.dataImage(40*40, 小明)') }
]

function logonFn(params) {
    console.log(params);
    let { user } = param2Obj(params.url)

    let b = arr.find(item => {
        return item.user == user
    })
    if (b) {
        return {
            code: 201,
            res: '该账号已被注册'
        }
    } else {
        return {
            code: 200,
            res: '该账号允许被注册'
        }
    }
}

function zhuceFn(data) {

    let { text, name, pass, region } = JSON.parse(data.body)

    arr.push({ user: text, name: name, password: pass, power: region, headP: Mockjs.mock('Random.dataImage(40*40, ' + name + ')'), })

    return {
        code: 200,
        res: '注册成功'
    }
}

function getUserFn(data) {
    let token = data.body


    let obj = {}
    arr.map(item => {
        if (item.token == token) {
            obj = item
        }

        return obj

    })
    return {
        code: 200,
        res: '请求成功',
        data: obj
    }
}
export default arr